package com.scenic.rd_android.testview.canvas;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.nova.scenic.projectlibs.util.debug.MyLog;

/**
 * Created by scenic on 15/9/6.
 */
public class CustomSimpleView extends ViewGroup{
    private static final String TAG = "CustomSimpleView";

    public CustomSimpleView(Context context) {
        super(context);
    }

    public CustomSimpleView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomSimpleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        MyLog.i(TAG,"onLayout l " + l + " t " + t + " r " + r + " b " + b);
        MyLog.i(TAG,"onLayout r-l " + (r - l) + " b -t " + (b - t)  +
                        " getWidth " + getWidth() +
                        " getHeight " + getHeight() +
                        " getMeasuredWidth " + getMeasuredWidth() +
                        " getMeasuredHeight " + getMeasuredHeight()
        );

        if(getChildAt(0) != null){
            getChildAt(0).layout(0,0,getChildAt(0).getMeasuredWidth(),getChildAt(0).getMeasuredHeight());
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

//        setMeasuredDimension(200,200);
        MyLog.i(TAG,
                //"widthMode " + widthMode +
                "  widthSize " + widthSize +
               // "  heightMode " + heightMode +
                "  heightSize " + heightSize);

        if(getChildAt(0) != null){
            measureChild(getChildAt(0), widthMeasureSpec, heightMeasureSpec);
//            getChildAt(0).measure(widthMeasureSpec,heightMeasureSpec);
        }
    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        MyLog.i(TAG,"startTimer");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        MyLog.i(TAG, "onDraw");
        View child = getChildAt(0);
        if(child != null){
            int width = child.getWidth();
            int height = child.getHeight();
            int measureWidth = child.getMeasuredWidth();
            int measureHeight = child.getMeasuredHeight();
            MyLog.i(TAG,"child width and height " + width + "  " + height);
            MyLog.i(TAG,"child measureWidth and measureHeight " + measureWidth + "  " + measureHeight);
        }
    }
}
